
/***********************面板-start****************************/
var rowData=[];
var valMsgRemoveTime=2000;

//主列表参数面板
var homeThemeGridOpt={
    id:"homeThemeGrid",//渲染对象ID 控件的唯一标识
    //title:"测试wdfGrid",//表格标题名暂时不要使用该属性,因为该实现为完善
    autoWidth:true,//是否自适应宽度,该属性设置时,列的width属性将失效,要让列的width属性生效请关闭该属性并设置colFixed属性为true
    autoHeight:true,
    //colFixed:true,//固定列宽,该属性启动时列width属性有效,autoWidth属性无效
    className:"test",//表格样式类名
    //checkbox:true,//是否显示选中按钮
    isMulitCheck:true,//是否支持多选
    loadMsg:"正在加载数据请稍等",//表格加载显示信息
    rowData:rowData,
    // autoGridHeight:true,
    root:"data",//服务端返回的数据对象名
    url:basePath+"/store/list",
    param:{},//主题类型1:首页主题
    columns:[
        {
            hidden : false,// 是否隐藏该列
            label : "服务区",// 显示列名称
            name : "serviceAreaName",// 列字段属性名
            width : "60",// 列宽度
        },
        {
            hidden : false,// 是否隐藏该列
            label : "属性",// 显示列名称
            name : "direction",// 列字段属性名
            width : "40",// 列宽度
            formatter : function(columnValue) {
                return getOptionConstant(areaDirectionConstant,columnValue);
            },
        },
        {
            hidden : false,// 是否隐藏该列
            label : "点位",// 显示列名称
            name : "pointMainId",// 列字段属性名
            width : "60",// 列宽度
            formatter : function(columnValue) {
                return getOptionConstant(pointMainConstant,columnValue);
            },
        },
        {
            hidden : false,// 是否隐藏该列
            label : "商户名称",// 显示列名称
            name : "name",// 列字段属性名
            width : "60",// 列宽度
        },
        {
            hidden : false,// 是否隐藏该列
            label : "创建时间",// 显示列名称
            name : "createTime",// 列字段属性名
            width : "40",// 列宽度
        },
        {
            hidden : false,// 是否隐藏该列
            label : "最后更新时间",// 显示列名称
            name : "lastUpdateTime",// 列字段属性名
            width : "40",// 列宽度
        },
        {
            hidden : false,// 是否隐藏该列
            label : "操作",// 显示列名称
            name : "id",// 列字段属性名`
            width : "60",// 列宽度
            isOrder : true,// 是否排序
            isFmtHtml:true,//格式化是否返回html
            formatter : function(columnValue,rowData){
                var btns=[];
                var editBtn = $("<a class='gridedit_btn' style='color:#1e90ff;' >编辑</a>");
                var chakanBtn=$("<a class='gridview_btn' >查看</a>");
                var delBtn = $("<a class='griddel_btn' style='color:red;'>删除</a>");

                $(editBtn).bind("click", function () {
                    showBounced(1,rowData);
                });
                $(chakanBtn).bind("click", function () {
                    showBounced(2,rowData);
                });
                $(delBtn).bind("click", function () {
                    deletepointconfig(rowData.id,rowData.storeDeptId);
                });

                btns.push(editBtn);
                btns.push(chakanBtn);
                btns.push(delBtn);
                return btns;
            }
        }
    ],
    event:{
        readyLoad:function(opt,gridObj){//grid数据加载前执行
            $(".panel").css("display","none");

        },
        loaded: function (opt, gridObj,rowDatas) {//grid数据加载完成后执行
        },
        restGridHeight:function(gridHeight,gridObj){
            return gridHeight-$("#searchForm").height();
        },
        onRowMouse:function (rowData,gridObj) {
            return [];
        },
    }
};


//显示弹框面板
function showBounced(status,rowData){

    // 启用下拉列表
    $("#pointconfig_name_pannel").attr('disabled',false);
    $("#exeDivFoot").show();

    if(status==1){

        //加载编辑数据
        $(".evalutePannelTop_title").html("编辑点位配置");
        $("#pointconfig_id_pannel").val(rowData.id);
        $("#pointconfig_storeDeptId_pannel").val(rowData.storeDeptId),
        $("#pointconfig_name_pannel").val(rowData.name);
        initSelectPointMainList(1,rowData.pointMainId,rowData.name,rowData.areaDeptId,rowData.serviceAreaName,rowData.serviceAreaId,rowData.direction);
    }if (status==2)  {

        //加载查看数据
        $(".evalutePannelTop_title").html("查看点位配置");
        $("#pointconfig_id_pannel").val(rowData.id);
        $("#pointconfig_storeDeptId_pannel").val(rowData.storeDeptId),
        $("#pointconfig_name_pannel").val(rowData.name);
        initSelectPointMainList(2,rowData.pointMainId,rowData.name,rowData.areaDeptId,rowData.serviceAreaName,rowData.serviceAreaId,rowData.direction);
        //禁用下拉列表
        $("#pointconfig_name_pannel").attr('disabled',true);
        $("#exeDivFoot").hide();

    }if (status==3)  {

        //加载新增数据
        $(".evalutePannelTop_title").html("新增点位配置");
        $('#addForm')[0].reset();
        initSelectPointMainList(1,"","","","","","");
    }

    $(".evalutePannel").css("display","block");
}


//删除
function deletepointconfig(id,storeDeptId){

    layerConfirm("你确定要删除吗?",function () {
        $.wdfProgress.progress("请稍等...",$("body"));
        $.ajax({
            url:basePath+"/store/delete",
            data:{
                id:id,
                storeDeptId:storeDeptId
            },
            dataType:"json",
            type: "post",
            contentType:"application/x-www-form-urlencoded",
            beforeSend:null,
            success:function(data){

                $.wdfProgress.showMsg(data.msg,$("body"),true,function(eventObj){

                });

                if (data.status==1){
                    homeThemeGridOpt.keepPage = true;
                    $("#homeThemeGrid").wdfGrid("reload",homeThemeGridOpt);
                }
            },
            error:function (XMLHttpRequest, textStatus, errorThrown) {
                $.wdfProgress.showMsg("系统错误或网络延迟！",$("body"),true);
            }
        });
    });
}



// 提交弹框表单
function submit(){

    if(!$("#pointconfig_name_pannel").val()){
        layerMsg("请输入点位名称！");
        return;
    }

    if($("#pointconfig_pointMainId_select option:selected").val()){

        if(!$("#pointconfig_areaDeptId_select option:selected").val()){
            layerMsg("请选择服务区名称！");
            return;
        }

        if(!$("#pointconfig_serviceAreaId_select option:selected").val()){
            layerMsg("请选择服务区属性！");
            return;
        }
    }


    var param={
        id:$("#pointconfig_id_pannel").val(),
        pointMainId:$("#pointconfig_pointMainId_select option:selected").val(),
        serviceDeptId:$("#pointconfig_areaDeptId_select option:selected").val(),
        serviceAreaId:$("#pointconfig_serviceAreaId_select option:selected").val(),
        storeDeptId:$("#pointconfig_storeDeptId_pannel").val(),
        name:$("#pointconfig_name_pannel").val(),
    };

    var url;

    if(!$("#pointconfig_pointMainId_select option:selected").val()){

        if(!$("#pointconfig_id_pannel").val()){
            url = basePath+"/pointMain/save";
        }else {
            url = basePath+"/pointMain/update";
        }
    }else {
        if(!$("#pointconfig_id_pannel").val()){
            url = basePath+"/store/save";
        }else {
            url = basePath+"/store/update";
        }
    }

    $.post(url,param,function(data){
        if (data.status == 1) {

            if(!$("#pointconfig_pointMainId_select option:selected").val()){

                initLiPointMainList();
            }else{

                homeThemeGridOpt.keepPage = true;
                $("#homeThemeGrid").wdfGrid("reload",homeThemeGridOpt);
            }
            closeScorePannel();
        }else {
            alert(data.msg);
        }
    });
}


/***********************面板-end****************************/

/***********************通用下拉列表-start****************************/

// 初始化加载点位名称
function initSelectPointMainList(isInward,pointMainId,name,areaDeptId,areaDeptName, serviceAreaId,direction){

    if((pointMainId==""&&name!="")||(pointMainId==""&&name=="")){
        $("#pointconfig_areaDeptId_Div").hide();
        $("#pointconfig_serviceAreaId_Div").hide();
    }else {
        $("#pointconfig_areaDeptId_Div").show();
        $("#pointconfig_serviceAreaId_Div").show();
        initSelectAreaDeptIdList(isInward,areaDeptId,areaDeptName,serviceAreaId,direction);
    }


    $("#pointconfigPointMainId").html("");
    if(isInward==1){
        var html="<select id='pointconfig_pointMainId_select' name='pointMainId'>";
        var param={};
        $.post(basePath+"/pointMain/list",param,function(data){
            if (data.status == 1) {
                var datas = data.data;

                if((pointMainId==""&&name!="")||(pointMainId==""&&name=="")){
                    html=html+"<option value='' ";
                    if(pointMainId==""){
                        html=html+" selected='selected' ";
                    }
                    html=html+">空白</option>";
                }

                if((pointMainId==""&&name=="")||pointMainId!=""){
                    $(datas).each(function(i,data){
                        html=html+"<option value='"+data.id+"'";

                        if(pointMainId==data.id){
                            html=html+" selected='selected'";
                        }
                        html=html+">"+data.name+"</option>";
                    });
                }


                html=html+"</select>";
                $("#pointconfigPointMainId").html(html);

                //加载点击事件
                $("#pointconfig_pointMainId_select").on("change",function () {
                    var selectVal = $("#pointconfig_pointMainId_select option:selected").val();
                    if(selectVal){
                        $("#pointconfig_areaDeptId_Div").show();
                        $("#pointconfig_serviceAreaId_Div").show();

                        initSelectAreaDeptIdList(isInward,areaDeptId,areaDeptName,serviceAreaId,direction);
                    }else{
                        $("#pointconfig_areaDeptId_Div").hide();
                        $("#pointconfig_serviceAreaId_Div").hide();
                    }
                });

            }else {
                alert(data.msg);
            }
        });
    }else {
        var html="<input type='text' id='pointconfig_pointMainId_pannel' value='"+getOptionConstant(pointMainConstant,pointMainId)+"' disabled='disabled'/>";
        $("#pointconfigPointMainId").html(html);
    }
}


// 初始化加载点位名称
function initSelectAreaDeptIdList(isInward,areaDeptId,areaDeptName,serviceAreaId,direction){

    $("#pointconfigAreaDeptId").html("");
    if(isInward==1){
        var html="<select id='pointconfig_areaDeptId_select' name='areaDeptId'>";
        var param={type:3};
        $.post(basePath+"/department/listByCondition",param,function(data){
            if (data.status == 1) {
                var datas = data.data;
                html=html+"<option value='' ";
                if(areaDeptId==""){
                    html=html+" selected='selected' ";
                }
                html=html+">请选择服务区名称</option>";
                $(datas).each(function(i,data){
                    html=html+"<option value='"+data.id+"'";
                    if(areaDeptId==data.id){

                        html=html+" selected='selected'";
                    }
                    html=html+">"+data.name+"</option>";
                });
                html=html+"</select>";
                $("#pointconfigAreaDeptId").html(html);

                //加载点击事件
                $("#pointconfig_areaDeptId_select").on("change",function () {
                    var selectVal = $("#pointconfig_areaDeptId_select option:selected").val();

                    initSelectServiceAreaIdList(isInward,selectVal,serviceAreaId,direction);
                });

            }else {
                alert(data.msg);
            }
        });
    }else {
        var html="<input type='text' id='pointconfig_areaDeptId_pannel' value='"+areaDeptName+"' disabled='disabled'/>";
        $("#pointconfigAreaDeptId").html(html);
    }

    initSelectServiceAreaIdList(isInward,areaDeptId,serviceAreaId,direction);
}


// 初始化加载点位名称
function initSelectServiceAreaIdList(isInward,areaDeptId,serviceAreaId,direction){

    $("#pointconfigServiceAreaId").html("");
    if(isInward==1){
        var html="<select id='pointconfig_serviceAreaId_select' name='serviceAreaId'>";
        var param={areaDeptId:areaDeptId==""?-1:areaDeptId};
        $.post(basePath+"/serviceArea/list",param,function(data){
            if (data.status == 1) {
                var datas = data.data;
                html=html+"<option value='' ";
                if(serviceAreaId==""){
                    html=html+" selected='selected' ";
                }
                html=html+">请选择服务区属性</option>";
                $(datas).each(function(i,data){
                    html=html+"<option value='"+data.id+"'";
                    if(serviceAreaId==data.id){

                        html=html+" selected='selected'";
                    }
                    html=html+">"+getOptionConstant(areaDirectionConstant,data.direction)+"</option>";
                });
                html=html+"</select>";
                $("#pointconfigServiceAreaId").html(html);
            }else {
                alert(data.msg);
            }
        });
    }else {
        var html="<input type='text' id='pointconfig_serviceAreaId_pannel' value='"+getOptionConstant(areaDirectionConstant,direction)+"' disabled='disabled'/>";
        $("#pointconfigServiceAreaId").html(html);
    }
}


// 初始化加载点位名称
function initLiPointMainList(){
    $("#ulli-left").html("");
    var param={};
    $.post(basePath+"/pointMain/list",param,function(data){
        if (data.status == 1) {
            var datas = data.data;
            var html="<ul>";
            $(datas).each(function(i,data){
                html+="<li " +
                            "onclick=\"javascript:pointMainSearch('"+data.id+"',this);\" " +
                            "onmouseover=\"javascript:pointMainonmouseover('"+data.id+"','"+data.name+"',this);\"" +
                            " onmouseout=\"javascript:pointMainonmouseout('"+data.id+"','"+data.name+"',this);\">"+
                            data.name+
                            "<button type='button' onclick=\"javascript:editPointMain('"+data.id+"','"+data.name+"');\" style='display:none;position: relative;top: 2px;background: url(../resource/images/ic_edit.png) no-repeat left -56px;margin-left: 15px;height: 16px;width: 16px;border: 0px;'></button>"+
                            "<button type='button' onclick=\"javascript:deletePointMain('"+data.id+"');\" style='display:none;position: relative;top: 2px;background: url(../resource/images/ic_delete.png) no-repeat left -56px;margin-left: 10px;height: 16px;width: 16px;border: 0px;'></button>"+
                      "</li>";
            });
            html+="</ul>";
            $("#ulli-left").html(html);
        }else {
            alert(data.msg);
        }
    });
}




/***********************通用下拉列表-end****************************/

/***********************业务操作-start****************************/


//验证消息显示
function showMsg(msg,input,obj){
    var msgBar=$("<div class='msgBar'>"+msg+"</div>");
    $(".msgBar").remove();
    obj.html(msgBar);
    msgBar.animate({"opacity":1},500);
    $(input).data("showMsg",msgBar);
    $(input).addClass("error");
};


//鼠标悬浮进入时间
function pointMainonmouseover(pointMainId,name,obj) {

    if(pointMainId!=$('#pointconfig_pointMainId_SearchPanel').val()){
        $(obj).css("background","#D3D3D3");
    }
    $(obj).find("button").show();
}

//鼠标悬浮离开时间
function pointMainonmouseout(pointMainId,name,obj) {

    if(pointMainId!=$('#pointconfig_pointMainId_SearchPanel').val()){
        $(obj).css("background","white");
    }
    $(obj).find("button").hide();
}

//编辑一级点位
function editPointMain(id,name) {
    // 启用下拉列表
    $("#pointconfig_name_pannel").attr('disabled',false);
    $("#exeDivFoot").show();

    $(".evalutePannelTop_title").html("编辑点位配置");
    $("#pointconfig_id_pannel").val(id);
    $("#pointconfig_name_pannel").val(name);
    initSelectPointMainList(1,"",name,"","","","");

    $(".evalutePannel").css("display","block");
}



//删除一级点位
function deletePointMain(id) {

    layerConfirm("你确定要删除吗?",function () {
        $.wdfProgress.progress("请稍等...",$("body"));
        $.ajax({
            url:basePath+"/pointMain/delete",
            data:{
                id:id,
            },
            dataType:"json",
            type: "post",
            contentType:"application/x-www-form-urlencoded",
            beforeSend:null,
            success:function(data){

                $.wdfProgress.showMsg(data.msg,$("body"),true,function(eventObj){

                });

                if (data.status==1){
                    initLiPointMainList();
                }
            },
            error:function (XMLHttpRequest, textStatus, errorThrown) {
                $.wdfProgress.showMsg("系统错误或网络延迟！",$("body"),true);
            }
        });
    });
}


//条件查询
function pointMainSearch(pointMainId,obj) {
    //旧背景选中取消
    $("#ulli-left ul li").css("background","white");
    //新背景选中
    $(obj).css("background","#1D438A");
    $('#pointconfig_pointMainId_SearchPanel').val(pointMainId);
    search();
}



//条件查询
function search() {
    homeThemeGridOpt.param=$('#searchForm').serialize();
    $("#homeThemeGrid").wdfGrid("reload",homeThemeGridOpt);
}


//查询重置
function resetSearch() {
    $('#pointconfig_pointMainId_SearchPanel').val("");
    $('#searchForm')[0].reset();
    search();
    initLiPointMainList();
}



/***********************业务操作-end****************************/



/***********************页面初始化-start****************************/
$(document).ready(function() {
    homeThemeGridOpt.height = $(document).height() - $('#searchPanel').height()-20;
    $("#homeThemeGrid").wdfGrid("init",homeThemeGridOpt);
    initLiPointMainList();

});
/***********************页面初始化-end****************************/